/**
 * @description 登录中间件
 */

const { SuccessModel,ErrorModel } = require('../model/ResModel')
const { loginCheckFailInfo } = require('../model/Errorinfo')

//api登录验证
async function loginCheck(ctx,next) {  
  if(ctx.session&&ctx.session.userInfo) {
    //已登录
    await next()
    return
  }
  //未登录
  ctx.body = new ErrorModel(loginCheckFailInfo)
}

//页面登录验证
async function loginRedirect(ctx,next) {  
  if(ctx.session&&ctx.session.userInfo) {
    //已登录
    await next()
    return
  }
  //未登录
  const curUrl = ctx.url;
  ctx.redirect('/login?url='+ encodeURIComponent(curUrl))
}

module.exports = {
  loginCheck,
  loginRedirect
}